﻿<div class="layui-fluid layui-anim febs-anim" id="febs-detailSubject" lay-title="明细科目管理">
    <div class="layui-row layui-col-space8 febs-container">
        <div class="layui-col-md7 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="detailSubject-table-form" id="detailSubject-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md9 layui-col-sm9 layui-col-xs9">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm febs-form-item-require">项目</label>
                                        <div class="layui-input-inline">
                                            <div id="projectSearch"></div>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">主科目</label>
                                        <div class="layui-input-inline">
                                            <div id="mainSubjectSearch"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md3 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain  table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                     shiro:hasAnyPermissions="project:add,project:delete">
                                    <i class="layui-icon">&#xe875;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="detailSubjectTable" lay-data="{id: 'detailSubjectTable'}"></table>
                </div>
            </div>
        </div>
        <div class="layui-col-md5 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-header" id="form-header">新增明细科目</div>
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" action="" lay-filter="detailSubject-form">
                        <div class="layui-form-item febs-hide">
                            <label class="layui-form-label febs-form-item-require">明细科目ID：</label>
                            <div class="layui-input-block">
                                <input type="text" name="id" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label febs-form-item-require">科目名称：</label>
                            <div class="layui-input-block">
                                <input type="text" name="detailSubjectName" autocomplete="off" class="layui-input" minlength="2"
                                       maxlength="20" lay-verify="range">
                            </div>
                        </div>
                        <div class="layui-form-item" id="projectInput">
                            <label class="layui-form-label febs-form-item-require">所属项目：</label>
                            <div class="layui-input-block">
                               <div id="projectSelect"></div>
                            </div>
                        </div>
                        <div class="layui-form-item" id="mainSubjectInput">
                            <label class="layui-form-label febs-form-item-require">主科目：</label>
                            <div class="layui-input-block">
                                <div id="mainSubjectSelect"></div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label febs-form-item-require">类型：</label>
                            <div class="layui-input-block">
                                <div id="typeSelect"></div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">科目描述：</label>
                            <div class="layui-input-block">
                                <textarea name="remark" maxlength="100" class="layui-textarea"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item febs-hide">
                            <button type="reset" class="layui-btn" id="reset-form"></button>
                            <button class="layui-btn" lay-submit="" lay-filter="detailSubject-form-submit"
                                    id="submit-form"></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-footer">
                    <button class="layui-btn" id="submit">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="detailSubject-option">
    <span shiro:lacksPermission="project:update,project:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
    <a lay-event="edit" shiro:hasPermission="project:update"><i class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
    <a lay-event="del" shiro:hasPermission="project:delete"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdownMenu', 'jquery', 'laydate', 'febs', 'form', 'eleTree', 'table', 'validate', 'xmSelect'], function () {
        let $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdownMenu = layui.dropdownMenu,
            validate = layui.validate,
            $view = $('#febs-detailSubject'),
            $query = $view.find('#query'),
            $submit = $view.find('#submit'),
            $searchForm = $view.find('#detailSubject-table-form'),
            $header = $view.find('#form-header'),
            projectSearchSelect,
            mainSubjectSearchSelect,
            projectSelect,
            mainSubjectSelect,
            typeXmSelect,
            projectData,
            tableIns;

        form.verify(validate);
        form.render();

        initTable();
        typeSelect();

        laydate.render({
            elem: '#createTime',
            range: true
        });

        mainSubjectSearchSelect = xmSelect.render({
            el: '#mainSubjectSearch',
            radio: true,
            clickClose: true,
            filterable: true,
            paging: true,
            pageSize: 5,
            name: 'mainSubjectId',
            model: {label: {type: 'text'}}
        })

        function typeSelect() {
            typeXmSelect = xmSelect.render({
                el: '#typeSelect',
                radio: true,
                clickClose: true,
                filterable: true,
                paging: true,
                pageSize: 5,
                name: 'type',
                model: {label: {type: 'text'}},
                data: [
                    {name: '收入', value: 1},
                    {name: '支出', value: -1},
                    {name: '仅记录', value: 0}
                ]
            })
        }
        projectSearchSelect = xmSelect.render({
            el: '#projectSearch',
            radio: true,
            clickClose: true,
            filterable: true,
            paging: true,
            pageSize: 5,
            name: 'projectId',
            model: {label: {type: 'text'}},
            on: function(data){
                let arr = data.arr;
                if (arr.len !== 0) {
                    febs.get(ctx + 'subject/' + arr[0].value, null, function (result) {
                        let mainSubjectList = result.data;
                        let data = [];
                        for (let i = 0; i < mainSubjectList.length; i++) {
                            let e = {};
                            e.value = mainSubjectList[i].id;
                            e.name = mainSubjectList[i].subjectName;
                            data[i] = e;
                        }
                        mainSubjectSearchSelect.update(
                            {data: data}
                        )
                    })
                }
                console.log(arr);
            }
        })

        febs.get(ctx + 'project', null, function (result) {
            let projectList = result.data;
            let data = [];
            for (let i = 0; i < projectList.length; i++) {
                let e = {};
                e.value = projectList[i].id;
                e.name = projectList[i].projectName;
                data[i] = e;
            }
            projectData = data;
            projectSearchSelect.update(
                {data: data}
            )
        })

        mainSubjectSelect = xmSelect.render({
            el: '#mainSubjectSelect',
            radio: true,
            clickClose: true,
            filterable: true,
            paging: true,
            pageSize: 5,
            name: 'subjectId',
            model: {label: {type: 'text'}}
        })

        projectSelect = xmSelect.render({
            el: '#projectSelect',
            radio: true,
            clickClose: true,
            filterable: true,
            paging: true,
            pageSize: 5,
            name: 'projectId',
            model: {label: {type: 'text'}},
            on: function(data){
                let arr = data.arr;
                if (arr.len !== 0) {
                    febs.get(ctx + 'subject/' + arr[0].value, null, function (result) {
                        let mainSubjectList = result.data;
                        let data = [];
                        for (let i = 0; i < mainSubjectList.length; i++) {
                            let e = {};
                            e.value = mainSubjectList[i].id;
                            e.name = mainSubjectList[i].subjectName;
                            data[i] = e;
                        }
                        mainSubjectSelect.update(
                            {data: data}
                        )
                    })
                }
                console.log(arr);
            }
        })

        febs.get(ctx + 'project', null, function (result) {
            let projectList = result.data;
            let data = [];
            for (let i = 0; i < projectList.length; i++) {
                let e = {};
                e.value = projectList[i].id;
                e.name = projectList[i].projectName;
                data[i] = e;
            }
            projectSelect.update(
                {data: data}
            )
        })



        dropdownMenu.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                let checkStatus = table.checkStatus('detailSubjectTable');
                if (name === 'add') {
                    resetdetailSubjectForm();
                    febs.alert.info("请在表单中填写相关信息");
                }
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        febs.alert.warn('请选择需要删除的明细科目');
                    } else {
                        febs.modal.confirm('删除明细科目', '确定删除所选明细科目？', function () {
                            let detailSubjectIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                detailSubjectIds.push(item.id);
                            });
                            deletedetailSubjects(detailSubjectIds.join(','));
                        });
                    }
                }
            },
            options: [{
                name: 'add',
                title: '新增明细科目',
                perms: 'project:add'
            }, {
                name: 'delete',
                title: '删除明细科目',
                perms: 'project:delete'
            }]
        });

        table.on('tool(detailSubjectTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'edit') {
                $header.text('修改明细科目');
                disalbeSelect();
                console.log(data)
                form.val("detailSubject-form", {
                    "id": data.id,
                    "detailSubjectName": data.detailSubjectName,
                    "remark": data.remark
                });
                typeXmSelect.setValue([data.type]);
                disalbeSelect();
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除明细科目', '确定删除该明细科目？', function () {
                    deletedetailSubjects(data.id);
                });
            }
        });

        $query.on('click', function () {
            let params = $searchForm.serializeJson();
            if (!params.projectId) {
                febs.alert.error('请选择项目');
                return;
            }
            resetdetailSubjectForm();
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, url: ctx + 'detailSubject/list'});
        });

        $submit.on('click', function () {
            $view.find('#submit-form').trigger('click');
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'detailSubjectTable',
                //url: ctx + 'detailSubject/list',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'detailSubjectName', title: '明细科目名称', minWidth: 120},
                    {field: 'projectName', title: '项目名称', minWidth: 100},
                    {field: 'mainSubjectName', title: '主科目名称', minWidth: 60},
                    {field: 'type', title: '费用类型', minWidth: 60, templet: function (data) {
                            if (data.type === 0) {
                                return "仅记录";
                            } else if (data.type === 1) {
                                return "收入";
                            } else {
                                return "支出";
                            }
                        }},
                    {field: 'remark', title: '描述', minWidth: 100},
                    {title: '操作', toolbar: '#detailSubject-option', width: 100}
                ]],
                data: []
            });
        }

        function getQueryParams() {
            let params = $searchForm.serializeJson();
            params.invalidate_ie_cache = new Date();
            return params;
        }

        function resetdetailSubjectForm() {
            $view.find('#reset-form').trigger('click');
            $header.text('新增明细科目');
            enalbeSelect();
        }

        form.on('submit(detailSubject-form-submit)', function (data) {
            console.log(data)
            let fields = data.field;
            if ($header.text() === '新增明细科目') {
                if (!fields.projectId
                    ||!fields.subjectId) {
                    febs.alert.error('缺少必要参数');

                    return false;
                }
            }
            if (!fields.detailSubjectName
                 || !fields.type) {
                febs.alert.error('缺少必要参数');
                return false;
            }
            addOrUpdatedetailSubject(fields);
            return false;
        });

        function deletedetailSubjects(detailSubjectIds) {
            febs.get(ctx + 'detailSubject/delete/' + detailSubjectIds, null, function () {
                febs.alert.success('删除明细科目成功');
                $query.trigger('click');
            })
        }

        let addOrUpdatedetailSubject = function (data) {
            if (data.id && $header.text() === '修改明细科目') {
                febs.post(ctx + 'detailSubject/update', data, function () {
                    febs.alert.success('修改明细科目成功');
                    $query.trigger('click');

                });
            } else {
                febs.post(ctx + 'detailSubject', data, function () {
                    febs.alert.success('新增明细科目成功');
                    resetdetailSubjectForm();
                    let params = $searchForm.serializeJson();
                    if (params.projectId) {
                        $query.trigger('click')
                    }
                });
            }
        }

        function disalbeSelect() {
            $view.find('#projectInput').addClass('febs-hide');
            $view.find('#mainSubjectInput').addClass('febs-hide');
        }
        function enalbeSelect() {
            $view.find('#projectInput').removeClass('febs-hide');
            $view.find('#mainSubjectInput').removeClass('febs-hide');
        }

    });
</script>